package com.example.demo.util.converters;

import com.example.demo.util.converters.impl.*;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 陈浩
 * @date 2021/1/9 14:25
 */
@Component
public class ConverterLoader {


    private static Map<String, MyConverter> allConverter = new HashMap<>();

    public static MyConverter getConverter(Class clazz) {
        return allConverter.get(ConverterKeyBuild.buildKey(clazz));
    }

    public static void initConverter(Class clazz, MyConverter myConverter) {
        allConverter.put(clazz.getName(), myConverter);
    }

    static {
        allConverter.put(ConverterKeyBuild.buildKey(BigDecimal.class), new BigDecimalMyConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Boolean.class), new BooleanConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Byte.class), new ByteMyConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Date.class), new DateConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Double.class), new DoubleMyConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Integer.class), new IntegerMyConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Float.class), new FloatMyConverter());
        allConverter.put(ConverterKeyBuild.buildKey(LocalDate.class), new LocalDateConverter());
        allConverter.put(ConverterKeyBuild.buildKey(LocalDateTime.class), new LocalDateTimeConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Long.class), new LongMyConverter());
        allConverter.put(ConverterKeyBuild.buildKey(Short.class), new ShortMyConverter());
        allConverter.put(ConverterKeyBuild.buildKey(String.class), new StringConverter());
    }
}
